package com.zhangxun.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangxun.domain.UserCollection;
import com.zhangxun.service.UserCollectionService;
import com.zhangxun.mapper.UserCollectionMapper;
import com.zhangxun.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 张循
* @description 针对表【user_collection】的数据库操作Service实现
* @createDate 2023-01-06 17:29:34
*/
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection>
    implements UserCollectionService{

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    public Boolean addOrUpdateById(Long prodId) {

        UserCollection userCollection = userCollectionMapper.selectOne(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, SecurityUtil.getWxUserId()));

        if(ObjectUtil.isNotNull(userCollection)){
            int i = userCollectionMapper.deleteById(userCollection.getId());
            return i == 1;
        }
        userCollection = new UserCollection();
        userCollection.setProdId(prodId);
        userCollection.setUserId(SecurityUtil.getWxUserId());
        userCollection.setCreateTime(new Date());

        return userCollectionMapper.insert(userCollection) == 1;

    }
}




